//
//  ViewController.m
//  TableCellPushAnimation
//
//  Created by Misheral on 15/7/28.
//  Copyright (c) 2015年 lifecycle. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UINavigationControllerDelegate>

@property(nonatomic, weak) IBOutlet UITableView *table;

@property(nonatomic, strong) UIView *popView;
@property(nonatomic) CGRect forwardRect;
@property(nonatomic) CGPoint forwardLocation;

@property(nonatomic, strong) UIViewController *vc;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.table.delegate = self;
    self.table.dataSource = self;
    
    self.navigationController.navigationBar.translucent = NO;
     {
        self.popView = [[UIView alloc] initWithFrame:CGRectZero];
        self.popView.backgroundColor = [UIColor blueColor];
         self.popView.layer.anchorPoint = CGPointMake(0.5, 0.5);
        
        UIButton *aBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        aBtn.frame = CGRectMake(0, 0, 100, 50);
        [aBtn setTitle:@"关闭" forState:UIControlStateNormal];
        [aBtn addTarget:self action:@selector(closeBtnTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
        [self.popView addSubview:aBtn];
        [self.navigationController.view addSubview:self.popView];
        [self.navigationController.view bringSubviewToFront:self.popView];
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 15;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *const cellIdentifer = @"animationcell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer forIndexPath:indexPath];
    cell.layer.transform = CATransform3DIdentity;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
// !!!TODO:注意_vc的背景颜色不能设置成和window的颜色一样，否则看不到展开动画效果，只能看到nav的
    
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
    _vc = [[UIViewController alloc] init];
    _vc.view.frame = CGRectZero;
    _vc.view.backgroundColor = [UIColor redColor];
    _vc.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStylePlain target:self action:@selector(closeBtnTouchUpInside:)];
    _vc.navigationItem.title = @"qqqqq";
    
    CGRect frame = _vc.view.frame;
    frame.size.width = self.view.frame.size.width;
    frame.size.height = tableView.rowHeight;
    _vc.view.frame = frame;
    _vc.view.center = [self.view convertPoint:selectedCell.center fromView:tableView];
    self.forwardRect = _vc.view.frame;
    
    
    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
//        _vc.view.backgroundColor = [UIColor whiteColor];
        _vc.view.frame = self.view.bounds;
        [self.navigationController pushViewController:_vc animated:NO];
    } completion:^(BOOL finished) {
        _vc.view.alpha = 1.0;
    }];
    
    
    //view操作的，第一种修改frame
//    [self.navigationController.view bringSubviewToFront:self.popView];
//    __block CGRect frame = CGRectZero;
//    frame.size.width = [[UIScreen mainScreen] bounds].size.width;
//    frame.size.height = 0;
//    self.popView.frame = frame;
//    self.popView.center = [self.view convertPoint:selectedCell.center fromView:tableView];
//    self.forwardRect = self.popView.frame;
//    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
//        frame = [[UIScreen mainScreen] bounds];
//        self.popView.frame = frame;
//    } completion:^(BOOL finished) {
//        
//    }];
    
    //view操作第二种 修改tranform
//    CGRect bounds = [[UIScreen mainScreen] bounds];
//    self.popView.layer.transform = CATransform3DIdentity;
//    self.popView.frame = bounds;
//    self.popView.center = [self.view convertPoint:selectedCell.center fromView:tableView];
//    self.forwardLocation = self.popView.center;
//    self.popView.layer.transform = CATransform3DMakeRotation(M_PI_2, 1, 0, 0);
//
//    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
//        CGPoint point = CGPointZero;
//        point.x = bounds.size.width/2 - self.forwardLocation.x;
//        point.y = bounds.size.height/2 - self.forwardLocation.y;
//        self.popView.layer.transform =CATransform3DTranslate(CATransform3DIdentity, point.x, point.y, 0);
//    } completion:^(BOOL finished) {
//        
//    }];
 
}


- (void)closeBtnTouchUpInside:(id)sender{
    
    //第一种的返回
//    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
//        self.popView.frame = self.forwardRect;
//    } completion:^(BOOL finished) {
//        self.forwardRect = CGRectZero;
//    }];
//    
    //第二种的返回
    
    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        _vc.view.frame = self.forwardRect;
    } completion:^(BOOL finished) {
         [self.navigationController popViewControllerAnimated:NO];
        self.forwardRect = CGRectZero;
    }];
}

@end
